package com.tastyshop.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;




import com.tastyshop.domain.UserReviewBoard;
import com.tastyshop.service.UserReviewBoardService;


@Controller
public class UserReviewController {
	
	@Autowired
	private UserReviewBoardService service;
	
	// 처음보여지는 리스트 화면
	@RequestMapping(value="userReview.do", method = RequestMethod.GET)
	public String userReview(HttpServletRequest request, Model model){
		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		int rows = 10;

		String field = "";
		if (request.getParameter("field") != null) {
			field = request.getParameter("field");
		}

		String keyword = "";
		if (request.getParameter("keyword") != null) {
			keyword = request.getParameter("keyword");
		}

		if ("".equals(keyword)) {
			field = "";
		}

		List<UserReviewBoard> userReviewBoards = service.getUserReviewBoardList(field, keyword,
				page, rows);
		int cnt = userReviewBoards.size();
		System.out.println(cnt);
		String totCnt = "";
		totCnt = userReviewBoards.get(0).getTOT_CNT();
		int viewCnt = 10;
		int totalCnt = Integer.parseInt(totCnt);
		int pageCnt = totalCnt / viewCnt + (totalCnt % viewCnt == 0 ? 0 : 1);
		int startPage = 1;
		if (page % 10 != 0) {
			startPage = (int) (page / 10) * 10 + 1;
		} else {
			startPage = ((int) (page / 10) - 1) * 10 + 1;
		}
		int totalPage = (int) Math.ceil((totalCnt / (double) viewCnt));
		int pageBlock = 10;
		int endPage = startPage + pageBlock - 1;
		if (endPage > pageCnt)
			endPage = pageCnt;
		
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("cnt", cnt);
		model.addAttribute("userReviewBoards", userReviewBoards);
		model.addAttribute("totCnt", totCnt);
		model.addAttribute("keyword", keyword);
		model.addAttribute("field", field);
		model.addAttribute("page", page);
		model.addAttribute("startPage", startPage);
		model.addAttribute("endPage", endPage);
		return "userReview";
	}
	
	// 상세페이지
	@RequestMapping(value="review_detail.do", method = RequestMethod.GET)
	public String review_detail(){
		return "review_detail";
	}

	@RequestMapping(value = "review_detail.do")
	public String detail(@RequestParam("idx") int idx, Model model){
		HashMap<String, UserReviewBoard> map = service.getInfo(idx);
		model.addAttribute("userReviewBoard", map.get("userReviewBoard"));
		model.addAttribute("prev", map.get("prev"));
		model.addAttribute("next", map.get("next"));
		service.hitCount(idx);
		return "review_detail";
	}
	
	@RequestMapping(value = "ur_write.do", method = RequestMethod.GET)
	public String userreview_insert(@RequestParam(value = "idx", required = false) Integer idx,
			@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
			@RequestParam(value = "action") String action, Model model, HttpSession session){
		session.getAttribute("email");
		
		if(action.equals("new")){
			if(session.getAttribute("email") == null){
				return "login";
			}
			model.addAttribute("title", "새 글 작성");
			model.addAttribute("board", new UserReviewBoard());
		}else if(action.equals("update")){
			model.addAttribute("title", "편집");
			model.addAttribute("board", service.getUserReviewBoardByIdx(idx));
		}else{
			throw new RuntimeException();
		}
		model.addAttribute("page", page);
		model.addAttribute("action", action);
		return "ur_write";
	}

	
}
